//
//  UserProductViewController.m
//  Finance
//
//  Created by Hu Jianguo on 13-4-18.
//  Copyright (c) 2013年 cheng xiang. All rights reserved.
//

#import "UserProductViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "AnalysisJson.h"
#import "MultiThreading.h"
#import "AppDelegate.h"
#import "UserProductCell.h"
#import "NearbyDetailViewController.h"
@interface UserProductViewController ()

@end

@implementation UserProductViewController
@synthesize dataArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    [self.navigationItem setLeftBarButtonItem:leftItem];
    
    userProductDict = [[NSDictionary alloc] init];
    
    productTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    productTableView.delegate = self;
    productTableView.dataSource = self;
    productTableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    UIColor* bk = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"beij.png"]];
    
    [productTableView setBackgroundColor:bk];
    
    [self.view addSubview:productTableView];
    
    dataArray = [[NSMutableArray alloc] init];
    [self loadUserPoductInfo];
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma 加载用户关注的产品信息
-(void)loadUserPoductInfo
{
    AppDelegate* mydelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (mydelegate.loginSessionID == nil)
    {
        return;
    }
    
    NSLog(@"session id = %@",[mydelegate loginSessionID]);
    //组请求报文
    NSDictionary *header = [AnalysisJson PackageJsonHeader:@"W108"];
    NSDictionary *body = [self setUserProductBody:0 PageIndex:0 SessionID:[mydelegate loginSessionID]];
    
    NSData *requestJson = [AnalysisJson PackageJsonFromHeader:header andBody:body];
    NSString *jsonString = [[NSString alloc] initWithData:requestJson encoding:NSUTF8StringEncoding];
    
    NSLog(@"%@",jsonString);
    
    //发送报文
    [self getUserProductInfo: requestJson];
}

-(NSDictionary*)setUserProductBody:(NSInteger)pageSize PageIndex:(NSInteger)pageIndex
SessionID:(NSString*)sessionId
{
    NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
    [dict setObject:[NSString stringWithFormat:@"%d",pageSize] forKey:@"pageSize"];
    [dict setObject:[NSString stringWithFormat:@"%d",pageIndex] forKey:@"pageIndex"];
    [dict setObject:sessionId  forKey:@"sessionId"];
    
    return dict;
}

-(void)getUserProductInfo:(NSData*)jsonData
{
    [MultiThreading getDataFromNetwork:[SERVICE_URL stringByAppendingString:@"QueryUserAttention"] toObject:self callBack:@selector(getUerProductInfoCallBack:) withBody:jsonData];
}

-(void)getUerProductInfoCallBack:(NSDictionary*)dic
{    
    NSDictionary* header = dic[@"header"];
    NSLog(@"header=%@",header);
    
    NSDictionary* body = dic[@"body"];
    if(header != nil && body.count ==0)
    {
        [self showMessageBox:@"提示" content:header[@"returnMessage"]];
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    

    if([body isKindOfClass:[NSDictionary class]] && [(NSDictionary *)body objectForKey:@"products"] != nil)
    {
        self.dataArray = [(NSDictionary *)body objectForKey:@"products"];
    }
    
    [productTableView reloadData];
}

-(void)showMessageBox:(NSString*)title content:(NSString*)content
{
    UIAlertView* msg = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [msg show];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
  
    UserProductCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UserProductCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *dic = [dataArray objectAtIndex:[indexPath row]];
    [cell drawCell:dic];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = [self.dataArray objectAtIndex:[indexPath row]];
    NearbyDetailViewController *aController = [[NearbyDetailViewController alloc] init];
    aController.proId = [dic objectForKey:@"prodId"];
    aController.showfocusbtn = 1;
    [self.navigationController pushViewController:aController animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate


@end
